package com.zmn.brs.common.constant;

import com.zmn.brs.common.enums.EventTypeEnum;
import com.zmn.brs.common.exception.BrsException;
import com.zmn.common.utils.date.DateUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * 类描述：redis key
 *
 * @author xujie
 * @since 2021/03/17 9:31
 * @modifier zhangyao 2021/04/22 修改getFrequencyStatistics方法
 */
public class RedisKeyConst {

    /**
     * 冒号分隔符 (:)
     */
    public static final String COLON = ":";

    /**
     * 空数据标识
     */
    public static final String NO_DATA = "NO DATA";

    /**
     * 员工操作频次统计
     */
    private static final String STAFF_OPERATE_FREQUENCY_STATISTICS = "brs:frequencyStatistics:staffId:";

    /**
     * 对象手机号操作频次统计
     */
    private static final String OBJECT_MOBILE_OPERATE_FREQUENCY_STATISTICS = "brs:frequencyStatistics:objectMobile:";

    /**
     * 对象IP操作频次统计
     */
    private static final String OBJECT_IP_OPERATE_FREQUENCY_STATISTICS = "brs:frequencyStatistics:staffId:objectIp:";

    /**
     * 获取频次统计key
     *
     *
     * @param gate 下单入口
     * @param objectIdentityAttribute 对象身份属性
     * @param objectIdentityAttributeValue 对象身份属性值
     * @param eventCode 事件编码
     * @param thresholdId 阀值id
     * @return
     * @modifier zhangyao 2021/04/22 修改参数Integer staffId为Long objectId，增加参数gate,objectIdentityAttribute
     */
    public static String getFrequencyStatistics(Integer gate, Integer objectIdentityAttribute,
                                                String objectIdentityAttributeValue, Integer eventCode,
                                                Integer thresholdId) {
        String operateFrequencyStatistics;
        if (objectIdentityAttribute == null || objectIdentityAttribute == BrsConst.OBJECT_IDENTITY_ATTRIBUTE_ID) {
            operateFrequencyStatistics = STAFF_OPERATE_FREQUENCY_STATISTICS;
        } else if (objectIdentityAttribute == BrsConst.OBJECT_IDENTITY_ATTRIBUTE_MOBILE) {
            operateFrequencyStatistics = OBJECT_MOBILE_OPERATE_FREQUENCY_STATISTICS;
        } else if (objectIdentityAttribute == BrsConst.OBJECT_IDENTITY_ATTRIBUTE_IP) {
            operateFrequencyStatistics = OBJECT_IP_OPERATE_FREQUENCY_STATISTICS;
        } else {
            throw new BrsException("对象身份属性不存在");
        }
        if (gate != null) {
            // 下单入口不为空，加上下单入口
            operateFrequencyStatistics += gate + COLON;
        }
        return operateFrequencyStatistics + objectIdentityAttributeValue + COLON + eventCode + COLON + thresholdId;
    }

    /**
     * 描述: redis 自增key 按天配置
     *
     * @author zhangyao
     * @since 2021/04/29
     **/
    public static final String BLACKLIST_SINCE_IP_REDIS_DAY_KEY_IP = "brs:blacklist:since_day_redis_key_ip:";
    public static final String BLACKLIST_SINCE_IP_REDIS_DAY_KEY_MOBILE = "brs:blacklist:since_day_redis_key_mobile:";
    public static final String BLACKLIST_SINCE_IP_REDIS_DAY_KEY_USER_ID = "brs:blacklist:since_day_redis_key_user_id:";
    public static final String BLACKLIST_IP_LIST = "brs:blacklist:ip:list";

    /**
     * 描述: redis 自增key 按时间配置
     *
     * @author zhangyao
     * @since 2021/04/29
     **/
    public static final String BLACKLIST_SINCE_IP_REDIS_TIME_KEY_IP = "brs:blacklist:since_time_redis_key_ip_";
    public static final String BLACKLIST_SINCE_IP_REDIS_TIME_KEY_MOBILE = "brs:blacklist:since_time_redis_key_mobile_";
    public static final String BLACKLIST_SINCE_IP_REDIS_TIME_KEY_USER_ID = "brs:blacklist:since_time_redis_key_user_id_";

    /**
     * 描述: redis key 按天配置
     *
     * @author chongw
     * @since 2021/04/29
     **/
    //当天城市对应推送次数
    public static final String QUANTITY_TODAY_CITY_SEND_COUNT_REDIS_KEY = "brs:quantity:today_city_send_count_redis_key:";
    //当天城市对应推送次数
    public static final String COMPARISON_TODAY_CITY_SEND_COUNT_REDIS_KEY = "brs:comparison:today_city_send_count_redis_key:";
    //规则配置redisKey
    private static final String RULE_CONFIG_VALUE_REDIS_KEY = "brs:%s:config_redis_key:%s";
    //缓存规则当天数据redisKey
    private static final String RULE_TODAY_VALUE_REDIS_KEY = "brs:%s:today_redis_key_v2:%s";
    //当天钉钉推送推送次数
    public static final String TODAY_DINGTALK_SEND_COUNT_REDIS_KEY = "brs:%s:today_send_count_redis_key:%s";

    /**
     * 获取配置 人单比 redis缓存 key
     *
     * @param eventType 事件类型
     * @param args 唯一值
     * @return
     */
    public static String getCacheRuleConfigRedisKey(Integer eventType , Object ... args){
        String uniqueNo = StringUtils.join(args, ":");
        return String.format(RULE_CONFIG_VALUE_REDIS_KEY, EventTypeEnum.getEventTypeEnum(eventType).name().toLowerCase() , uniqueNo);
    }

    /**
     * 业务当日数据 redis缓存 key
     *
     * @param eventType 事件类型
     * @param args 唯一值
     * @return
     */
    public static String getTodayRedisKey(Integer eventType , Object ... args){
        String uniqueNo = StringUtils.join(args, ":");
        return String.format(RULE_TODAY_VALUE_REDIS_KEY, EventTypeEnum.getEventTypeEnum(eventType).name().toLowerCase() , uniqueNo);
    }

    /**
     * 当天钉钉推送次数 redis缓存 key
     *
     * @param eventType 事件类型
     * @param args 唯一值
     * @return
     */
    public static String getTodayDingtalkRedisKey(Integer eventType , Object ... args){
        String uniqueNo = StringUtils.join(args, ":");
        return String.format(TODAY_DINGTALK_SEND_COUNT_REDIS_KEY, EventTypeEnum.getEventTypeEnum(eventType).name().toLowerCase() , uniqueNo);
    }

    /**
     * 场景、事件、规则状态
     */
    //场景
    public static final String SCENE_STATUS_REDIS_KEY = "brs:scene:status_redis_key:";

    //事件
    public static final String EVENT_STATUS_REDIS_KEY = "brs:event:status_redis_key:";

    //规则
    public static final String RULE_STATUS_REDIS_KEY = "brs:rule:status_redis_key:";

    //阈值
    public static final String THRESHOLD_STATUS_REDIS_KEY = "brs:threshold:status_redis_key:";

    /**
     * 状态redis key
     *
     * @param ruleId
     * @return
     */
    public static String getStatusRedisKey(String prefix,Integer ruleId){
        return prefix + ruleId;
    }


    /**
     *  整体单量城市预警推送次数 redis缓存 key
     *
     * @param cityId
     * @return
     */
    public static String getTodayQuantityDingtalkCountRedisKey(Integer cityId){

        return QUANTITY_TODAY_CITY_SEND_COUNT_REDIS_KEY + DateUtil.getNowFormatted(DateUtil.FORMAT_DATE2) + ":" + cityId;
    }

    /**
     *  人单比城市预警推送次数 redis缓存 key
     *
     * @param cityId
     * @return
     */
    public static String getTodayComparisonDingtalkCountRedisKey(Integer cityId, Integer servGroupId){

        return COMPARISON_TODAY_CITY_SEND_COUNT_REDIS_KEY + DateUtil.getNowFormatted(DateUtil.FORMAT_DATE2) + ":" + cityId + ":" + servGroupId;
    }
}
